package com.szw.dlyk.result;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @program: dlyk
 * @description: web层返回对象
 * @author: Songzw
 * @create: 2024-01-21 18:45
 **/
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Result {

    /**
     * 表示返回的结果码，比如200成功，500失败
     */
    private int code;

    /**
     * 表示返回的结果信息，比如 用户登录状态失效了，请求参数格式有误.......
     */
    private String msg;

    /**
     * 表示返回的结果数据，数据可能是一个对象，也可以是一个List集合.....
     */
    private Object data;

    public static Result ok(){
        return Result.builder()
                .code(CodeEnum.OK.getCode())
                .msg(CodeEnum.OK.getMsg())
                .build();
    }

    public static Result ok(int code,String msg){
        return Result.builder()
                .code(code)
                .msg(msg)
                .build();
    }

    public static Result ok(Object data){
        return Result.builder()
                .code(CodeEnum.OK.getCode())
                .msg(CodeEnum.OK.getMsg())
                .data(data)
                .build();
    }

    public static Result ok(CodeEnum code){
        return Result.builder()
                .code(code.getCode())
                .msg(code.getMsg())
                .build();
    }

    public static Result fail(){
        return Result.builder()
                .code(CodeEnum.FAIL.getCode())
                .msg(CodeEnum.FAIL.getMsg())
                .build();
    }

    public static Result fail(String msg){
        return Result.builder()
                .code(CodeEnum.FAIL.getCode())
                .msg(msg)
                .build();
    }

    public static Result fail(CodeEnum codeEnum){
        return Result.builder()
                .code(codeEnum.getCode())
                .msg(codeEnum.getMsg())
                .build();
    }
}
